#!/bin/bash
set -e
if [ -z "${MountPoint}" ]; then
    EnvFile="../common/0_env.sh"
    # shellcheck source=../common/0_env.sh
    source ${EnvFile}
fi

echo -e "\n***********************************************************************"
echo -e "\t\t$0 : 测试orcafs-ctl --du --uid/gid 与 系统 du 是否一致"
echo -e "***********************************************************************"

execMode=$1
fileNum=5
vdbDirPath="/test_du_uid"
absVdbDirPath="${MountPoint}${vdbDirPath}"
mkdir -p "${absVdbDirPath}"
orcafs-ctl --setpattern --storagepoolid=2 "${absVdbDirPath}" &> /dev/null

uid=`cat /etc/passwd | grep "${SysUser}" | awk -F: '{print $3}'`
gid=`cat /etc/passwd | grep "${SysUser}" | awk -F: '{print $4}'`

function create_data()
{
    # 构造数据
    echo -e "\n构造数据 ${vdbDirPath}"
    rm_abs_dir ${vdbDirPath}
    mkdir -p "${absVdbDirPath}"
    for((i=0;i<${fileNum};i++)); do
        dd if=/dev/zero of="${absVdbDirPath}/file${i}" bs=1000 count=1 &> /dev/null
        mkdir -p "${absVdbDirPath}/dir${i}"
        dd if=/dev/zero of="${absVdbDirPath}/dir${i}/file${i}" bs=1000 count=1 &> /dev/null
    done

    echo -e "\t注意: 创建后需要 sleep 否则目录的大小无法准确更新, du 无法统计到目录本身的大小"
    echo -e "\t共创建 ${fileNum}*2 files, ${fileNum} dirs"
    sleep 1
}

# 测试没有 uid/gid orcafs-ctl --du 和 系统 du 是否一致
function test_du_without_id()
{
    echo -e "\n测试没有 uid/gid 是能否正确大小"
    if [ "${execMode}" == "--fastmode" ]; then
        sysDuSize="10015"
        allFileSize="10000"
    else
        sysDuSize=`du -sb "${absVdbDirPath}" | awk '{print $1}'`
        allFileSize=`find "${absVdbDirPath}" ! -type d | xargs stat --format=%s | \
                    awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'`
    fi
    allDirSize=$[${sysDuSize} - ${allFileSize}]

    test_du_size_all ${vdbDirPath} "${sysDuSize}"
    test_du_size_file ${vdbDirPath} "${allFileSize}"
    test_du_size_dir ${vdbDirPath} ${allDirSize}
}

# 测试有 uid/gid 是能否正确计算文件和目录本身大小
function test_du_with_id()
{
    echo -e "\n测试有 uid/gid 是能否正确计算总大小"
    # 修改文件属性
    chown "${uid}":"${gid}" "${absVdbDirPath}"/file{1,2,3}
    chown "${uid}":"${gid}" "${absVdbDirPath}/dir0/file0"

    # 修改目录
    chown "${uid}":"${gid}" "${absVdbDirPath}"/dir{1,2,3}

    if [ "${execMode}" == "--fastmode" ]; then
        uidAllSize="4003"
        gidAllSize="4003"

        uidFileSize="4000"
        gidFileSize="4000"

        uidDirSize="3"
        gidDirSize="3"
    else
        # 此种方法不能获取 absVdbDirPath 目录本身的大小
        uidAllSize=`find "${absVdbDirPath}" -uid "${uid}" | xargs stat --format=%s | \
            awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'`
        gidAllSize=`find "${absVdbDirPath}" -gid "${gid}" | xargs stat --format=%s | \
            awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'`

        uidFileSize=`find "${absVdbDirPath}" -uid "${uid}" ! -type d | xargs stat --format=%s | \
            awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'`
        gidFileSize=`find "${absVdbDirPath}" -gid "${gid}" ! -type d | xargs stat --format=%s | \
            awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'`
        
        uidDirSize=`find "${absVdbDirPath}" -uid "${uid}" -type d | xargs stat --format=%s | \
            awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'`
        gidDirSize=`find "${absVdbDirPath}" -gid "${gid}" -type d | xargs stat --format=%s | \
            awk 'BEGIN{sum=0} {sum+=$1} END{print sum}'`
    fi
    
    # 总大小
    test_du_size_all ${vdbDirPath} "${uidAllSize}" --uid="${uid}"
    test_du_size_all ${vdbDirPath} "${gidAllSize}" --gid="${gid}"

    # 文件
    echo -e "\n测试有 uid/gid 是能否正确计算文件大小"
    test_du_size_file ${vdbDirPath} "${uidFileSize}" --uid="${uid}"
    test_du_size_file ${vdbDirPath} "${gidFileSize}" --gid="${gid}"

    # 目录
    echo -e "\n测试有 uid/gid 是能否正确计算目录本身大小"
    test_du_size_dir ${vdbDirPath} "${uidDirSize}" --uid="${uid}"
    test_du_size_dir ${vdbDirPath} "${gidDirSize}" --gid="${gid}"
}

create_data
test_du_without_id
test_du_with_id
rm_abs_dir "${absVdbDirPath}"
